/*
 * ResList.java
 *
 * Created on 2005/01/23 +0900
 *
 * $Revision: 38 $
 * $Date: 2008-10-11 08:48:45 +0000 (Sat, 11 Oct 2008) $
 */

package org.gikolet.bbs.data;

import java.util.Enumeration;
import java.util.Hashtable;

/**
 * @author Tetsutaro (latest modification by $Author: tetsutaro.ss $)
 * @version $Revision: 38 $ $Date: 2008-10-11 08:48:45 +0000 (Sat, 11 Oct 2008) $
 */
public class ResList {
    private Hashtable reses;

    public ResList() {
	this.reses = new Hashtable();
    }

    public synchronized void set(Res res) {
	this.reses.put(new Integer(res.getNo()), res);
    }

    public synchronized void setRange(ResList reses) {
	int[] keys = reses.getKeys();
	for (int i = 0; i < keys.length; i++) {
	    set(reses.get(keys[i]));
	}
    }

    public synchronized Res get(int no) {
	return (Res) this.reses.get(new Integer(no));
    }

    public synchronized int[] getKeys() {
	int[] wi = new int[this.reses.size()];

	int i = 0;
	Enumeration e = this.reses.keys();
	while (e.hasMoreElements()) {
	    Integer oi = (Integer) e.nextElement();
	    wi[i] = oi.intValue();
	    i++;
	}
	return wi;
    }

    public synchronized void clear() {
	this.reses.clear();
    }

    public synchronized int getStartIndex(int index) {
	if (this.reses.containsKey(new Integer(index))) {
	    int i = index - 1;
	    while (true) {
		if (!this.reses.containsKey(new Integer(i))) {
		    return i + 1;
		}
		i--;
	    }
	}
	return -1;
    }

    public synchronized int getEndIndex(int index) {
	if (this.reses.containsKey(new Integer(index))) {
	    int i = index + 1;
	    while (true) {
		if (!this.reses.containsKey(new Integer(i))) {
		    return i - 1;
		}
		i++;
	    }
	}
	return -1;
    }

    public synchronized boolean containsIndex(int index) {
	return this.reses.containsKey(new Integer(index));
    }
}
